package com.example.smartcity4.ui.user;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.smartcity4.MainActivity;
import com.example.smartcity4.R;
import com.example.smartcity4.javaBean.GsonBean;
import com.example.smartcity4.javaBean.QuestBean;
import com.example.smartcity4.reteofi.SmartService;
import com.google.gson.Gson;

import okhttp3.MediaType;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class QuestActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText text1;
    private EditText text2;
    private Button btn;
    private TextView text3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_quest);
        initView();
    }

    private void initView() {


        text3 = findViewById(R.id.text3);

        text1 = (EditText) findViewById(R.id.text1);
        text2 = (EditText) findViewById(R.id.text2);
        btn = (Button) findViewById(R.id.btn);
text2.addTextChangedListener(new TextWatcher() {
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {
        int i = s.length();
        text3.setText(i+"/");
    }
});
        btn.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn:
                submit();
                break;
        }
    }

    private void submit() {
        // validate
        String text1String = text1.getText().toString().trim();
        if (TextUtils.isEmpty(text1String)) {
            Toast.makeText(this, "请输入标题", Toast.LENGTH_SHORT).show();
            return;
        }

        String text2String = text2.getText().toString().trim();
        if (TextUtils.isEmpty(text2String)) {
            Toast.makeText(this, "请输入反馈内容", Toast.LENGTH_SHORT).show();
            return;
        }

        // TODO validate success, do something

        Retrofit retrofit = new Retrofit.Builder().baseUrl(MainActivity.host)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        SmartService service = retrofit.create(SmartService.class);
        QuestBean bean=new QuestBean(text1String,text2String);
        Gson gson  = new Gson();
        String json = gson.toJson(bean);
        RequestBody body = RequestBody.create(MediaType.parse("application/json"), json);

        Call<GsonBean> call = service.post_quest(body,MainActivity.token);
        call.enqueue(new Callback<GsonBean>() {
            @Override
            public void onResponse(Call<GsonBean> call, Response<GsonBean> response) {
                if (Integer.valueOf(response.body().code)==200){
                    Toast.makeText(getApplicationContext(),"提交成功", Toast.LENGTH_SHORT).show();
                }else {
                    Toast.makeText(getApplicationContext(),"提交失败", Toast.LENGTH_SHORT).show();
                }
                //
            }

            @Override
            public void onFailure(Call<GsonBean> call, Throwable throwable) {

            }
        });

    }
}